package com.jf.dao.impl;

import com.jf.bean.Enterprise;
import com.jf.dao.EnterpriseDao;
import com.jf.util.BaseDao;
import com.jf.util.MyUtils;

import java.util.ArrayList;
import java.util.List;

public class EnterpriseDaoImpl implements EnterpriseDao {
    @Override
    public Enterprise getById(int id) {
        String sql = "select * from enterprise where id = ?";
        return BaseDao.baseQueryBean(Enterprise.class,sql,id);
    }

    @Override
    public int update(Enterprise enterprise) {
        List<Object> objs = new ArrayList<>();
        String sql = "update enterprise set ";
        if(MyUtils.isStringNotEmpty(enterprise.getName())){
            sql+="name = ?,";
            objs.add(enterprise.getName());
        }
        if(MyUtils.isStringNotEmpty(enterprise.getHeaderLogo())){
            sql+="header_logo = ?,";
            objs.add(enterprise.getHeaderLogo());
        }
        if(MyUtils.isStringNotEmpty(enterprise.getFooterLogo())){
            sql+="footer_logo = ?,";
            objs.add(enterprise.getFooterLogo());
        }
        if(MyUtils.isStringNotEmpty(enterprise.getBrand())){
            sql+="brand = ?,";
            objs.add(enterprise.getBrand());
        }
        if(MyUtils.isStringNotEmpty(enterprise.getContacts())){
            sql+="contacts = ?,";
            objs.add(enterprise.getContacts());
        }
        if(MyUtils.isStringNotEmpty(enterprise.getPhone())){
            sql+="phone = ?,";
            objs.add(enterprise.getPhone());
        }
        if(MyUtils.isStringNotEmpty(enterprise.getAddress())){
            sql+="address = ?,";
            objs.add(enterprise.getAddress());
        }
        if(MyUtils.isStringNotEmpty(enterprise.getQrCode())){
            sql+="qr_code = ?,";
            objs.add(enterprise.getQrCode());
        }
        if(MyUtils.isStringNotEmpty(enterprise.getQrInfo())){
            sql+="qr_info = ?,";
            objs.add(enterprise.getQrInfo());
        }
        if(MyUtils.isStringNotEmpty(enterprise.getEnterpriseInfo())){
            sql+="enterprise_info = ?,";
            objs.add(enterprise.getEnterpriseInfo());
        }
        if(MyUtils.isStringNotEmpty(enterprise.getEnterpriseImg())){
            sql+="enterprise_img = ?,";
            objs.add(enterprise.getEnterpriseImg());
        }
        sql=sql.substring(0,sql.lastIndexOf(","));
        sql+=" where id = ?";
        objs.add(enterprise.getId());
        return BaseDao.baseUpdate(sql,objs);
    }
}
